//
//  ViewController.m
//  08_Homework
//
//  Created by 徐昊 on 16/7/25.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Frame.h"
#import "StudentViewController.h"
#import "AddStudentViewController.h"
#define kTableViewCellName @"reuseIndentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, AddStudentDelegate>
@property (nonatomic, strong) NSMutableArray *studentArray;
@property (nonatomic, strong) UITableView *student;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // navigationController设置
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleIn:)];
    // 数据处理
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    _studentArray = [NSMutableArray arrayWithContentsOfFile:path];
    NSLog(@"%@", _studentArray);
    // 创建tableView
    _student = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_student];
    _student.dataSource = self;
    _student.delegate = self;
    [_student registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellName];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]}];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_student setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        NSLog(@"完成");
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.修改数据源
    [_studentArray removeObjectAtIndex:indexPath.row];
    // 2.对应修改cell
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)handleIn:(UIButton *)button {
    AddStudentViewController *vc = [[AddStudentViewController alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
// delegate传值必须实现的方法
- (void)addStudent:(NSDictionary *)student {
    if (![[student objectForKey:@"name"] isEqualToString:@""]) {
        [_studentArray addObject:student];
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
    [_student reloadData];
}
#pragma mark - UITableView Delegate
// 自定义头视图
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.h_width, 30)];
//    label.text = @"通讯录";
//    label.font = [UIFont systemFontOfSize:20 weight:20];
//    label.textAlignment = NSTextAlignmentCenter;
//    return label;
//}
// 头视图高度
//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
//    return 50;
//}
#pragma mark - UITableView DataSource
// cell数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _studentArray.count;
}
// cell内容 (重用池)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellName forIndexPath:indexPath];
    cell.textLabel.text = [_studentArray[indexPath.row] objectForKey:@"name"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StudentViewController *vc = [[StudentViewController alloc] init];
    vc.name = [_studentArray[indexPath.row] objectForKey:@"name"];
    NSLog(@"%@", vc.age);
    vc.gender = [_studentArray[indexPath.row] objectForKey:@"gender"];
    vc.age = [[_studentArray[indexPath.row] objectForKey:@"age"] description];
    vc.phoneNumber = [_studentArray[indexPath.row] objectForKey:@"phoneNumber"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - Memory
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
